﻿using MvvmCross.Platform;
using MvvmCross.Plugins.Messenger;
using MvvmCross.Core.ViewModels;
using Sidvall.AuthorizationManager.UI.Messaging;
using System.Windows.Input;

namespace Sidvall.AuthorizationManager.UI.ViewModels
{
	public class NavigationViewModel : Sidvall.UI.ViewModels.ViewModelBase
	{
		#region Public Members

		#region CanShowStart

		private bool _CanShowStart;
		public bool CanShowStart
		{
			get
			{
				return _CanShowStart;
			}
			set
			{
				_CanShowStart = value;
				RaisePropertyChanged("CanShowStart");
			}
		}

		#endregion
		#region CanShowAuthorizationScopes

		private bool _CanShowAuthorizationScopes;
		public bool CanShowAuthorizationScopes
		{
			get
			{
				return _CanShowAuthorizationScopes;
			}
			set
			{
				_CanShowAuthorizationScopes = value;
				RaisePropertyChanged("CanShowAuthorizationScopes");
			}
		}

		#endregion
		#region ShowStartCommand

		public ICommand ShowStartCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<DashboardViewModel>());
			}
		}

		#endregion
		#region ShowAuthorizationScopesCommand

		public ICommand ShowAuthorizationScopesCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<AuthorizationScopeViewModel>());
			}
		}

		#endregion

		#region Start

		public override void Start()
		{
			var messenger = Mvx.Resolve<IMvxMessenger>();
			this.ViewModelResources.Add(messenger.Subscribe<ServerManagerMessage>(OnServerManagerMessage));
			InitializeCommands();
		}

		#endregion

		#endregion
		#region Private  Members

		#region InitializeCommands

		private void InitializeCommands()
		{
			if (Sidvall.AuthorizationManager.UI.SystemContext.Current.ServerManagers == null)
			{
				this.CanShowStart = false;
				this.CanShowAuthorizationScopes = false;
			}
			else if (Sidvall.AuthorizationManager.UI.SystemContext.Current.ServerManagers.IsValid)
			{
				this.CanShowStart = true;
				this.CanShowAuthorizationScopes = true;
			}
			else
			{
				this.CanShowStart = false;
				this.CanShowAuthorizationScopes = false;
			}
		}

		#endregion

		#region OnServerManagerMessage

		private void OnServerManagerMessage(ServerManagerMessage item)
		{
			InitializeCommands();
		}

		#endregion

		#endregion
	}
}
